package com.wqtz.chechai.view.activities;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Handler;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.LoanDetailInfoAdapter;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.LoanDetailInfoBean;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.LogUtils;
import com.wqtz.chechai.utils.StringUtils;
import com.yinglan.swiperefresh.PullToRefreshBase;
import com.yinglan.swiperefresh.PullToRefreshSwipeListView;
import com.yinglan.swiperefresh.SwipeMenu;
import com.yinglan.swiperefresh.SwipeMenuCreator;
import com.yinglan.swiperefresh.SwipeMenuItem;
import com.yinglan.swiperefresh.SwipeMenuListView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称： CheChai
 * 创建人 :  Kisum
 * 创建时间: 2017/1/22
 * 借款详情页面
 */
public class LoanDetailInfoActivity extends BaseActivity {

    //本金
    private TextView mTvLoaninfoPrincipal;
    //利息
    private TextView mTvLoaninfoInterest;
    //回退本金
    private TextView mTvLoaninfoRebackPrincipal;
    //预计还款时间
    private TextView mTvLoaninfoRepaymentTime;
    //确定
    private Button mBtnConfirm;
    private ImageView ivBack;
    /**
     * 需要展示的LoanDetailInfoBean数据
     */
    private List<LoanDetailInfoBean> mDatas;
    private PullToRefreshSwipeListView mPullRefreshListView;
    int requsetDataNum = 0;
    private LoanDetailInfoAdapter mAdapter;

    @Override
    public void init() {
        mPullRefreshListView = (PullToRefreshSwipeListView) findViewById(R.id.pull_to_refresh_list);
        mDatas = new ArrayList<>();
        mAdapter = new LoanDetailInfoAdapter(this, R.layout.item_loan_detail);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        triggerLoadData(10, true);
        mPullRefreshListView.setAdapter(mAdapter);
        mPullRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showMyDialog(position);
            }
        });
        mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<SwipeMenuListView>() {
            //下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoanDetailInfoActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
                        //触发加载数据
                        triggerLoadData(10, true);

                    }
                }, 2000);
            }

            //上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoanDetailInfoActivity.this, "加载更多", Toast.LENGTH_SHORT).show();
                        //触发加载数据
                        triggerLoadData(10, false);

                    }
                }, 2000);
            }
        });
        mPullRefreshListView.getRefreshableView().setOnMenuItemClickListener(mAdapter);
        //创建侧滑菜单
        mPullRefreshListView.getRefreshableView().setMenuCreator(creator);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
    }

    @Override
    public void loadData() {

    }

    @Override
    public int setContentViews() {
        return R.layout.activity_loan_detail;
    }

    @Override
    public BasePresent createPresenter() {
        return null;
    }


    private void triggerLoadData(int data, boolean isResfresh) {
        mDatas.clear();
        if (isResfresh) {
            requsetDataNum = 4;
        } else {
            requsetDataNum += data;
        }
        //模拟加载数据
        for (int i = 0; i < requsetDataNum; i++) {
            LoanDetailInfoBean loanDetailInfoBean = new LoanDetailInfoBean();
            if (i==0){
                loanDetailInfoBean.setLoanDetailType(0);//最新一期
            }else {
                loanDetailInfoBean.setLoanDetailType(1);//过去几期
            }
            loanDetailInfoBean.setLoanDetailInfo("19.00万/15天/利率0.5%");
            loanDetailInfoBean.setLoanDetailTime("16-12-09 22:30");
            loanDetailInfoBean.setLoanDetailPeriods(requsetDataNum-i);
            mDatas.add(loanDetailInfoBean);
            //EventBus
            EventBus.getDefault().post(loanDetailInfoBean);
        }
//        loadDatas();
        mAdapter.clearData();
        mAdapter.addData(mDatas);
        mPullRefreshListView.onRefreshComplete();
    }

    private SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
            createMenu1(menu);
        }

        /**
         * 创建删除菜单
         * @param menu
         */
        private void createMenu1(SwipeMenu menu) {
            SwipeMenuItem item1 = new SwipeMenuItem(
                    LoanDetailInfoActivity.this);
            item1.setBackground(R.mipmap.menu_del);
            item1.setWidth(dp2px(90));
            item1.setTitleColor(Color.WHITE);
            item1.setTitleSize(18);
            item1.setTitle(R.string.delete);
            menu.addMenuItem(item1);

        }


        private int dp2px(int dp) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                    getResources().getDisplayMetrics());
        }
    };

    /**
     * 弹出借款信息对话框
     */
    public void showMyDialog(int position) {
        LayoutInflater inflater = getLayoutInflater();
        final View myDialog = inflater.inflate(R.layout.dialog_loan_info, (ViewGroup) findViewById(R.id.dialog_loan_info_detail));
        mTvLoaninfoPrincipal = (TextView) myDialog.findViewById(R.id.tv_loaninfo_principal);
        mTvLoaninfoInterest = (TextView) myDialog.findViewById(R.id.tv_loaninfo_interest);
        mTvLoaninfoRebackPrincipal = (TextView) myDialog.findViewById(R.id.tv_loaninfo_reback_principal);
        mTvLoaninfoRepaymentTime = (TextView) myDialog.findViewById(R.id.tv_loaninfo_repayment_time);
        mBtnConfirm = (Button) myDialog.findViewById(R.id.btn_confirm);
        LoanDetailInfoBean loanDetailInfoBean = mDatas.get(position);
        mTvLoaninfoPrincipal.setText(loanDetailInfoBean.getLoanDetailInfo().substring(0,6));
        mTvLoaninfoInterest.setText("950元");
        mTvLoaninfoRebackPrincipal.setText("0.00元");
//        String dayStr = loanDetailInfoBean.getLoanDetailTime().substring(6, 8);
//        int dayInt = Integer.parseInt(dayStr);
//        String dayStr2 = loanDetailInfoBean.getLoanDetailInfo().substring(7, 9);
//        int dayInt2 = Integer.parseInt(dayStr2);
        mTvLoaninfoRepaymentTime.setText("16-12-24");
        final Dialog dialog = new Dialog(this);
        //加载自定义的布局
        dialog.setContentView(myDialog);
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }


    @Override
    protected void onResume() {
        super.onResume();
        activitys.add(this); //添加当前的activity
    }
}
